part of '../widgets.dart';

/// [VanCheckbox]属性常量
const kVanCheckboxIconSize = VanCssKey._raw('--van-checkbox-icon-size');
const kVanCheckboxIconColor = VanCssKey._raw('--van-checkbox-icon-color');
const kVanCheckboxDuration = VanCssKey._raw('--van-checkbox-duration');
const kVanCheckboxLabelMargin = VanCssKey._raw('--van-checkbox-label-margin');
const kVanCheckboxLabelColor = VanCssKey._raw('--van-checkbox-label-color');
const kVanCheckboxCheckedIconColor = VanCssKey._raw('--van-checkbox-checked-icon-color');
const kVanCheckboxDisabledIconColor = VanCssKey._raw('--van-checkbox-disabled-icon-color');
const kVanCheckboxDisabledLabelColor = VanCssKey._raw('--van-checkbox-disabled-label-color');


/// [VanCheckbox]主题类
@immutable
class VanCheckboxTheme extends VanProvider<VanCheckboxThemeData> with VanProvide {
  const VanCheckboxTheme({super.key, required super.child, required super.provide});

  static VanCheckboxThemeData of(BuildContext context) => VanProvider.of<VanCheckboxTheme>(context)?.provide ?? VanTheme.of(context).checkboxTheme;
  static const _prefix = "--van-checkbox-";
  static bool support(VanCssKey key) => key.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanCheckboxThemeData checkboxTheme = VanCheckboxTheme.of(context);
    if (kVanCheckboxIconSize == key) return checkboxTheme.iconSize.get(context);
    if (kVanCheckboxIconColor == key) return checkboxTheme.iconColor.get(context);
    if (kVanCheckboxDuration == key) return checkboxTheme.duration.get(context);
    if (kVanCheckboxLabelMargin == key) return checkboxTheme.labelMargin.get(context);
    if (kVanCheckboxLabelColor == key) return checkboxTheme.labelColor.get(context);
    if (kVanCheckboxCheckedIconColor == key) return checkboxTheme.checkedIconColor.get(context);
    if (kVanCheckboxDisabledIconColor == key) return checkboxTheme.disabledIconColor.get(context);
    if (kVanCheckboxDisabledLabelColor == key) return checkboxTheme.disabledLabelColor.get(context);
    return null;
  }
}

/// [VanCheckbox]主题数据类
@immutable
class VanCheckboxThemeData with VanProvide {
  final VanCss<double> iconSize;
  final VanCss<Color> iconColor;
  final VanCss<double> duration;
  final VanCss<double> labelMargin;
  final VanCss<Color> labelColor;
  final VanCss<Color> checkedIconColor;
  final VanCss<Color> disabledIconColor;
  final VanCss<Color> disabledLabelColor;

  /// 构造器
  const VanCheckboxThemeData.raw({
    required this.iconSize,
    required this.iconColor,
    required this.duration,
    required this.labelMargin,
    required this.labelColor,
    required this.checkedIconColor,
    required this.disabledIconColor,
    required this.disabledLabelColor,
  });

  /// 拷贝
  VanCheckboxThemeData copyFrom({
    VanCss<double>? iconSize,
    VanCss<Color>? iconColor,
    VanCss<double>? duration,
    VanCss<double>? labelMargin,
    VanCss<Color>? labelColor,
    VanCss<Color>? checkedIconColor,
    VanCss<Color>? disabledIconColor,
    VanCss<Color>? disabledLabelColor,
  }) {
    return VanCheckboxThemeData.raw(
      iconSize: iconSize ?? this.iconSize,
      iconColor: iconColor ?? this.iconColor,
      duration: duration ?? this.duration,
      labelMargin: labelMargin ?? this.labelMargin,
      labelColor: labelColor ?? this.labelColor,
      checkedIconColor: checkedIconColor ?? this.checkedIconColor,
      disabledIconColor: disabledIconColor ?? this.disabledIconColor,
      disabledLabelColor: disabledLabelColor ?? this.disabledLabelColor,
    );
  }

  /// 反序列化
  static VanCheckboxThemeData fromJson(Map<String, dynamic> json) {
    return VanCheckboxThemeData.raw(
      iconSize: VanCss(json[kVanCheckboxIconSize.key]),
      iconColor: VanCss(json[kVanCheckboxIconColor.key]),
      duration: VanCss(json[kVanCheckboxDuration.key]),
      labelMargin: VanCss(json[kVanCheckboxLabelMargin.key]),
      labelColor: VanCss(json[kVanCheckboxLabelColor.key]),
      checkedIconColor: VanCss(json[kVanCheckboxCheckedIconColor.key]),
      disabledIconColor: VanCss(json[kVanCheckboxDisabledIconColor.key]),
      disabledLabelColor: VanCss(json[kVanCheckboxDisabledLabelColor.key]),
    );
  }

}

/// [VanCheckbox]样式
@immutable
class VanCheckboxStyle with VanProvide {
  static of(BuildContext context) => VanProvider.of<VanCheckboxStyle>(context);

  final double? iconSize;
  final Color? iconColor;
  final double? duration;
  final double? labelMargin;
  final Color? labelColor;

  VanCheckboxStyle({
    this.iconSize,
    this.iconColor,
    this.duration,
    this.labelMargin,
    this.labelColor,
  });

  bool get isNonNull {
    if (iconSize == null) return false;
    if (iconColor == null) return false;
    if (duration == null) return false;
    if (labelMargin == null) return false;
    if (labelColor == null) return false;
    return true;
  }

  VanCheckboxStyle copyFrom({
    double? iconSize,
    Color? iconColor,
    double? duration,
    double? labelMargin,
    Color? labelColor,
  }) {
    return VanCheckboxStyle(
      iconSize: iconSize ?? this.iconSize,
      iconColor: iconColor ?? this.iconColor,
      duration: duration ?? this.duration,
      labelMargin: labelMargin ?? this.labelMargin,
      labelColor: labelColor ?? this.labelColor,
    );
  }
}